﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetBotLogic.Classes.BetRoundClasses;
using DotNetBotLogic.Classes.Log;
using DotNetBotLogic.Classes;
using DotNetBotLogic.Enums;
using DotNetBotLogic.Classes.PlayHistory;

namespace DotNetBotLogic.Controllers
{
    class PlayTrackerController : BaseController
    {
        #region Static

        private static bool busy = false;

        #endregion 

        #region Variables

        internal List<Player> PlayerList = new List<Player>();

        internal int NumberOfPFBets = 0;
        internal int NumberOfFBets = 0;
        internal int NumberOfTBets = 0;
        internal int NumberOfRBets = 0;
        internal int HandActions = 0;
        internal double MaxBet;

        internal int NumPlayers = 0;

        private PreflopTracker pfTracker;

        #endregion

        #region Constructor

        internal PlayTrackerController(MainController ctrlBase)
            : base()
        {
            pfTracker = new PreflopTracker(this);

        }

        #endregion

        internal void PrepareNewHand()
        {
            HandActions = 0;

            NumberOfPFBets = 0;
            NumberOfFBets = 0;
            NumberOfTBets = 0;
            NumberOfRBets = 0;
            NumPlayers = 0;
        }

        internal void PrepareNewBetRound()
        {
            MaxBet = (MainController.BetTurn == 1) ? BaseController.BBlind : 0;
        }

        internal void RefreshPlayersData()
        {
            Player player;
            string name;
            double balance = 0;

            Logger.Log(Environment.NewLine);
            for (int i = 0; i < 10; i++)
            {
                name = OhHelper.GetPlayerName(i);
                if (string.IsNullOrEmpty(name))
                    continue;

                balance = OhHelper.GetSymbol(string.Format("balance{0}", i)) + OhHelper.GetSymbol(string.Format("currentbet{0}", i));
                if (balance == 0)
                    continue;

                player = PlayerList.Where(a => a.Name == name).FirstOrDefault();
                if (player == null)
                {
                    player = new Player(name);
                    PlayerList.Add(player);
                }
                player.Clear();
                player.CurrentChair = i;
                player.DealPosition = (int)OhHelper.GetSymbol(string.Format("ac_dealpos{0}", i));
                player.Balance = balance;
                player.HandsPlayed++;

                NumPlayers++;

                Logger.Log(string.Format("PlayTracker -- {0} chair:{1} balance: {2}", player.Name, player.CurrentChair, player.Balance));
            }
            Logger.Log(Environment.NewLine);
        }

        internal void GetActionResult(bool isFromAlli)
        {
            if (busy)
                return;
            try
            {
                busy = true;
                if (isFromAlli && HandActions == 0)
                {
                    HandActions++;
                    RefreshPlayersData();
                    base.GetActionResult();
                }
                else if (HandActions > 0 && NumPlayers > 0)
                {
                    base.GetActionResult();
                }
            }
            finally
            {
                busy = false;
            }
        }

        protected override BetRoundBase Preflop()
        {
            pfTracker.TrackData();
            return null;
        }

        protected override BetRoundBase Flop()
        {
            return null;
        }

        protected override BetRoundBase Turn()
        {
            return null;
        }

        protected override BetRoundBase River()
        {
            return null;
        }
    }
}
